  //
//  BYUserManager.m
//  ZhiHuiLiJiang
//
//  Created by 胡忠诚 on 2018/11/21.
//  Copyright © 2018 WisdomLijiang. All rights reserved.
//用户信息管理

#import "BYUserManager.h"

@implementation BYUserManager
BYSingtonM(BYUserManager)

- (void)requestLoginParams:(NSDictionary *)params completion:(loginBlock)completion{
    [BYNetworking requestGetOrPostWithType:POST url:LJURL_sms_login params:params success:^(NSInteger code, NSString * _Nonnull msg, id  _Nonnull data) {
        
        if (code == 0) {
            NSLog(@"登录成功");
            NSDictionary *dict =[data objectForKey:@"data"];
            
            BYUserData *curUserdata = [[BYUserData alloc]init];
            curUserdata.userToken = dict[@"token"];
            curUserdata.mobile =dict[@"user"][@"mobile"];
            curUserdata.username =dict[@"user"][@"username"];
            curUserdata.hasauth =dict[@"user"][@"hasauth"];
            curUserdata.status =dict[@"user"][@"status"];
            curUserdata.uuid = dict[@"user"][@"uuid"];
            self.curUserdata = curUserdata;
            
            [self saveUserInfo];//存储用户信息到本地偏好设置
            completion(YES,@"0",data);
        }
    } fail:^(NSError * _Nonnull error) {
        [UIView addMJNotifierWithText:@"登录失败！" dismissAutomatically:YES];
    } showHUD:YES];
}
- (void)requestlogout:(NSDictionary *)params logout:(loginBlock)completion{//退出登录
    [BYNetworking requestGetOrPostWithType:POST url:LJURL_User_logout params:params success:^(NSInteger code, NSString * _Nonnull msg, id  _Nonnull data) {
        
        if (code == 0) {
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//appIcon的角标数字清0
            [[UIApplication sharedApplication] unregisterForRemoteNotifications];//不再接收推送通知
            BYUserData *curUserdata = [[BYUserData alloc]init];
            curUserdata.mobile = @"";
            curUserdata.userToken =  @"";
            curUserdata.username =  @"";
            curUserdata.hasauth =  @"";
            curUserdata.status =  @"";
            curUserdata.uuid =  @"";
            self.curUserdata = curUserdata;
            [self removeAllSaveCache];//清空本地偏好设置中的用户信息
            completion(YES,@"0",data);
        }
    } fail:^(NSError * _Nonnull error) {
        [UIView addMJNotifierWithText:@"退出失败！" dismissAutomatically:YES];
    } showHUD:YES];
}
- (void)saveUserInfo{//存储用户信息到本地偏好设置
    [[NSUserDefaults standardUserDefaults] setObject:self.curUserdata.mobile forKey:currentUserPhone];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [SaveCacheData saveDataStr:self.curUserdata.userToken withKey:currentUserToken];
    [SaveCacheData saveDataStr:self.curUserdata.uuid withKey:currentUserudid];
    [SaveCacheData saveDataStr:self.curUserdata.hasauth withKey:currentUserhasuth];
    [SaveCacheData saveDataStr:self.curUserdata.status withKey:currentUserstate];
    [SaveCacheData saveDataStr:self.curUserdata.username withKey:currentUsername];
    [SaveCacheData saveDataStr:@"YES" withKey:loginState];
}
- (void)removeAllSaveCache{//清空本地偏好设置中的用户信息
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:currentUserPhone];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [SaveCacheData saveDataStr:@"" withKey:currentUserToken];
    [SaveCacheData saveDataStr:@"" withKey:currentUserudid];
    [SaveCacheData saveDataStr:@"" withKey:currentUserhasuth];
    [SaveCacheData saveDataStr:@"" withKey:currentUserstate];
    [SaveCacheData saveDataStr:@"" withKey:currentUsername];
    [SaveCacheData saveDataStr:@"NO" withKey:loginState];
}

@end
